package com.zz.controller;

import com.zz.domain.CourseTopic;
import com.zz.domain.CourseVideo;
import com.zz.services.CourseTopicService;
import com.zz.services.CourseVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class CourseVideoController {
    @Autowired
    CourseTopicService courseTopicService ;
    @Autowired
    CourseVideoService courseVideoService ;

    //根据课程专题id号找到 课程专题 和 所有的视频
    @RequestMapping("/courseVideo/{topicId}")
    public String courseVideo(Model model, @PathVariable Integer topicId ){
        CourseTopic topic = courseTopicService.getCourseById(topicId);
        List<CourseVideo> videoList  = courseVideoService.getVideoByTopicId(topicId);

        model.addAttribute("courseTopic",topic);   //程程专题
        model.addAttribute("videoList",videoList);  //专题所属的视频列表
        model.addAttribute("playVideo",videoList.get(0));
        return  "course_video";
    }

    //找对课程专题的某一个视频
    @RequestMapping("/courseVideo/{topicId}/{videoId}")
    public String courseVideoId(Model model, @PathVariable Integer topicId,@PathVariable Integer videoId ){
        System.out.println(topicId+"----"+videoId);
        CourseTopic topic = courseTopicService.getCourseById(topicId);
        List<CourseVideo> videoList  = courseVideoService.getVideoByTopicId(topicId);

        model.addAttribute("courseTopic",topic);   //程程专题
        model.addAttribute("videoList",videoList);  //专题所属的视频列表
        if (videoId == null ||videoId<0 || videoId >= videoList.size() ) videoId = 0 ;
        model.addAttribute("playVideo",videoList.get(videoId));
        return  "course_video";
    }



}
